import java.io.*;

/**
 * Class to represent a File object that can be sent
 * and recreated on another system
 * Will actually do the creating for you
 **/
public class FilePacket implements Serializable 
{
	private String m_dirName;
	private String m_fileName;
	private byte[] m_data;

	/**
	 * Make a file packet that represents a given filename
	 * @param name The filename this represents
	 **/
	public FilePacket( String dirName, String fileName )
	{
		m_dirName = dirName;
		m_fileName = fileName;
	}

	/**
	 * Get the name associated with this file
	 * @return The name
	 **/
  	public String getFullName()
  	{
  		return m_dirName + m_fileName;
  	}
  	
  	public String getFileName()
  	{
  		return m_fileName;
  	}

	/**
	 * Have the FilePacket read itself in from the
	 * file it represents in name
	 **/
  	public void readIn()
  	{
	  	try
	  	{
	    	File file = new File( getFullName() );
	     	m_data = new byte[ (int)(file.length()) ];
	     	(new FileInputStream( file )).read( m_data );
	    }
	  	catch( FileNotFoundException fnfe )
	  	{
	  		System.err.println(fnfe.getMessage());
	  	}
	  	catch( Exception e )
	  	{
	    	e.printStackTrace();
	   	}
	}

	/**
	 * Have the file packet recreate itself, used
	 * after sending it to another location
	 * file will have same name and contents
	 * @param out The outputStream to write itself to
	 **/
	public void writeTo( OutputStream out )
	{
		try
		{
			out.write( m_data );
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
	}
}